Uploading files in NodeJS using filetor

In this article we will learn how to upload Single, Multiple files in NodeJS using filetor package by example. We will develope two API first for uploading single file (profileImg) in /register end-point and second end-point for uploading multiple files (cars) in /advertisment end-point.

Requirement

Installation

  • Create new project new directory uploader then inside this directory initiate new npm project by running the following command
npm init

After answering the questions a package.json file generated.

  • Install the following packages express which used for creating web apps and filetor which used for uploading files. By running the following command
npm i express filetor
  • Create app.js file which will be the main entry point for our app and public folder which will be used for uploading and serving static files.

In our entry point app.js we will create two API the first one /register post api which used for uploading a profile image when you are creating a new account. The second API /advertisment post end-point for selling car online and uploading multiple image for this car.

filetor

Accept an object as a prameter

filetor({file:PUT_HERE_FILE_NAME,dir:PUT_HERE_UPLOADED_DIRECTORY_PATH,allowedExtentions:AN_ARRAY_OF_ALLOWED_EXTENSIONS})
  • file This will be the file name which you want to upload for example req.files.profileImg

  • dir The path for uploaded directory for example /public/uploads

  • allowedExtentions An array of file extentions for example ["png","jpg","pdf"]

filetors

Accept an object as a prameter

filetors({files:ARRAY_OF_FILES,dir:PUT_HERE_UPLOADED_DIRECTORY_PATH,allowedExtentions:AN_ARRAY_OF_ALLOWED_EXTENSIONS})
  • files This will be an array of files which you want to upload for example req.files.cars

  • dir The path for uploaded directory for example /public/uploads

  • allowedExtentions An array of file extentions for example ["png","jpg","pdf"]

/*
 * Create instance from filetor package
 * filetorInit for initiating filetor with and integrating with express
 * filetor used for uploading a single file
 * filetors used for uploading multiple files
 */
const {filetor,filetors,filetorInit} = require("filetor");
const  express = require("express");
const  path = require("path");
const  app = express();
app.use(express.json());
app.use(express.urlencoded({extended:false}));
//initalize filetor
app.use(filetorInit({ limits: { fileSize:  10 * 1024 * 1024 },
}));
const  uploadDirectory = "public/uploads";
const  allowedExtentions = ["png","jpg","jpeg","GIF"];
app.use(express.static(path.join(__dirname, "public")));
//uploading single file using filetor instance
app.post("/register",async (req,res,next)=>{
    let  profileImage = req.files.profileImg;
    let upload = await  filetor({file:profileImage,dir:path.join(__dirname,uploadDirectory),allowedExtentions});    
    return  res.json(upload)
});
app.post("/advertisment",async (req,res,next)=>{
    let  cars = req.files.cars;
    let upload = await  filetors({files:cars,dir:path.join(__dirname,uploadDirectory),allowedExtentions});    
    return  res.json(upload)
});
app.listen(5000,()=>{
console.log(`Server is running http://localhost:5000`)
})

Testing by using postman

/register

enter image description here

/advertisment

enter image description here

Finally files uploaded successfully in uploads directory

enter image description here

Subscribe to our Newsletter

Subscribe to tech-hour website to get all updates, new articles, new courses and projects. No spam ever. Unsubscribe at any time.